import http from "@/api";

import { PORT1 } from "@/api/config/servicePort";

import type { MerakControlProps, MerakControlOption } from "@/components/MerakControl";

export type OptionType = MerakControlOption;

export interface InitParams {
  type_id?: string | number;
  product_id?: string | number;
  goods_id?: string | number;
  is_suite?: 0 | 1;
  is_copy?: 0 | 1;
}

// 创建初始化接口
export const getInitData = (params: InitParams, queueId?: string) =>
  http.post<
    Record<string, MerakControlProps[]> & {
      [k in "config" | "spec" | "form_value" | "form_readonly"]?: Record<string, any>;
    } & { [k in "product_id"]?: string | number }
  >(
    `${PORT1}/goods-archive/product-edit/index`,
    { ...params, is_copy: params.is_copy ?? !!params.product_id ? 1 : 0 },
    { queueId, loading: false }
  );

// 获取选项
export const getOptions = (type_id: string | number, prop_key: string, is_suite: 0 | 1, queueId?: string) =>
  http.post<MerakControlOption[]>(
    `${PORT1}/goods-archive/product-edit/get-propval`,
    { type_id, prop_key, is_suite },
    { queueId, loading: false, cancel: false }
  );

// 获取品牌和颜色
export const getBrandAndColor = (type_id: string | number, supplier_id: string | number, queueId?: string) =>
  http.post<{
    qf_code: string;
    brand: MerakControlOption[];
    color: MerakControlOption[];
  }>(`${PORT1}/goods-archive/product-edit/get-brand-color`, { type_id, supplier_id }, { queueId, loading: false });

// 验证商品系统型号
export const validUid = (spu: string) =>
  http.post(`${PORT1}/goods-archive/product-edit/verify-spu-unique`, { spu }, { loading: false });

// 验证商品货号
export const validsku = (sku: string) =>
  http.post(`${PORT1}/goods-archive/product-edit/verify-sku-unique`, { sku }, { loading: false });

// 保存数据
export const saveData = (data: Record<string, any>) =>
  http.post(`${PORT1}/goods-archive/product-edit/create-save`, { ...data }, { loading: false });

// 保存编辑数据
export const saveEditData = (data: Record<string, any>) =>
  http.post(`${PORT1}/goods-archive/product-edit/save-goodsinfo`, { ...data }, { loading: false });

// 获取部件信息
export const getParts = (product_id: Record<string, any>) =>
  http.post(`${PORT1}/goods-archive/product-edit/get-parts `, { product_id }, { loading: false });

// 获取部件信息
export const getPartsInfo = (product_id: Record<string, any>) =>
  http.post(`${PORT1}/goods-archive/product-edit/part-info`, { product_id }, { loading: false });

// 提交
export const saveParts = (data: Record<string, any>) =>
  http.post(`${PORT1}/goods-archive/product-edit/save-part-info`, { ...data }, { loading: false });

// 获取部件信息
export const getFacility = (product_id: Record<string, any>) =>
  http.post(`${PORT1}/goods-archive/product-edit/get-facility`, { product_id }, { loading: false });
